﻿using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Eqwww.Domain.Models.QuartzJob;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Eqwww.QuartzJob.ExecuteTask.Sample
{
    public class HelloJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                
                var scheduleId = context.MergedJobDataMap.Get("scheduleId").ToString();
                string connstr = context.MergedJobDataMap.Get("connstr").ToString();
                string contentroot = context.MergedJobDataMap.Get("contentroot").ToString();
                DbHelper.connstring = connstr;
                var filename = DateTime.Now.ToString("yyyyMMddHHmmss") + "_auto.bak";
                var filepath = "/upload/dbbackup/" + filename;
                var savepath = contentroot+filepath.Replace("/", "\\");
                var sql = string.Format("backup database {0} to disk ='{1}'", "EqwwwCore31", savepath);
                DbHelper.ExecuteSqlCommand(sql);

                sql = string.Format(@"INSERT INTO [dbo].[Sys_DbBackups]
           ([Id]
           ,[CreateUserId]
           ,[CreateTime]
           ,[LastUpdateUserId]
           ,[LastUpdateTime]
           ,[DeleteMark]
           ,[DeleteUserId]
           ,[DeleteTime]
           ,[DbName]
           ,[FileName]
           ,[FilePath]
           ,[FileSize]
           ,[BackupTime]
           ,[SortCode]
           ,[DbBackupType])
     VALUES
           ('{0}'
           ,null
           ,'{1}'
           ,null
           ,null
           ,null
           ,null
           ,null
           ,'Qian2019'
           ,'{2}'
           ,'{3}'
           ,'{4}'
           ,'{5}'
           ,null
           ,0) ", System.Guid.NewGuid().ToString(), DateTime.Now.ToString(), filename, filepath, Eqwww.Code.Common.GetFileSizeStr(Eqwww.Code.FileHelper.GetFileSize(savepath)), DateTime.Now.ToString());
                DbHelper.ExecuteSqlCommand(sql);

                sql = string.Format(@"INSERT INTO [dbo].[Quartz_ScheduleLogs]
           ([Id]
           ,[TableName]
           ,[Describe]
           ,[Type]
           ,[CreateTime]
           ,[UpdateTime]
           ,[ScheduleId]
           ,[OperatorId]
           ,[Operator])
     VALUES
           ('{0}'
           ,'Quartz_Schedules'
           ,'{1}'
           ,0
           ,'{2}'
           ,null
           ,'{3}'
           ,null
           ,null)", System.Guid.NewGuid().ToString(), "自动备份成功,文件目录:"+filepath, DateTime.Now.ToString(), scheduleId);
                DbHelper.ExecuteSqlCommand(sql);

                return Task.CompletedTask;
            }
            catch(Exception ex)
            {
                var filename = System.Guid.NewGuid().ToString();
                StreamWriter sw = new StreamWriter(@"d:\HelloJob_Err_" + filename + ".txt", false, System.Text.Encoding.GetEncoding("utf-8"));
                sw.Write(ex.Message);
                sw.Close();
                return Task.CompletedTask;
            }
            
        }
    }
}